package cn.wanda.bi.sunshine.platform.server.schedule;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.wanda.bi.sunshine.platform.server.bean.model.DimColumnLabelValue;
import cn.wanda.bi.sunshine.platform.server.client.response.elasticsearch.UserLabelBean;
import cn.wanda.bi.sunshine.platform.server.client.response.elasticsearch.UserLabelBucket;
import cn.wanda.bi.sunshine.platform.server.service.LabelDictionaryService;

/**
 * Created by James on 17/7/24.
 */
@Component
public class LabelValueSchedule {

    @Autowired
    private LabelDictionaryService userBaseLabelService;
    
    //每天凌晨1点执行
    @Scheduled(cron = "0 0 1 * * ?")
    public void run() {
        List<String> labels = new ArrayList<>();
        labels.add("sex");
        labels.add("education");
        labels.add("city_name");
        labels.add("city_id");
        labels.add("card_grade");
        labels.add("geo_plaza_id_1");
        labels.add("geo_plaza_name_1");
        labels.add("geo_plaza_id_2");
        labels.add("geo_plaza_name_2");
        labels.add("plaza_id_consume");
        labels.add("plaza_name_consume");
        labels.add("level_1_name_1");
        labels.add("level_1_name_2");
        labels.add("level_1_name_3");
        labels.add("order_code_name_1");
        labels.add("order_code_name_2");
        labels.add("order_code_name_3");
        labels.add("category_1_name_1");
        labels.add("category_1_name_2");
        labels.add("category_1_name_3");
        labels.add("category_name_1_tied");
        labels.add("level_1_name_tied");
        labels.add("app_type");
        labels.add("plaza_id_visit");
        labels.add("plaza_name_visit");
        labels.add("product_line");
        labels.add("level_1_name_log_1");
        labels.add("level_1_name_log_2");
        labels.add("level_1_name_log_3");
        labels.add("category_1_name_log_1");
        labels.add("category_1_name_log_2");
        labels.add("category_1_name_log_3");
        labels.add("plaza_name_wifi");
        labels.add("plaza_id_wifi");
        labels.add("kuaiqian");
        labels.forEach(p -> {
            UserLabelBean userLabelBean = userBaseLabelService.QueryUserValue(p);
            List<UserLabelBucket> hits = userLabelBean.getAggregations().getDistinct_w().getBuckets();
            userBaseLabelService.deleteByLabel(p);
            hits.forEach(hit -> {
                DimColumnLabelValue value = new DimColumnLabelValue();
                value.setLabel(p);
                value.setValue(hit.getKey());
                userBaseLabelService.insert(value);
            });
        });
    }

}
